package com.lgm.ht.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.lgm.ht.dto.FruitDTO;
import com.lgm.ht.mapper.FruitMapper;
import com.lgm.ht.pojo.Fruit;
import com.lgm.ht.service.FruitService;
import com.lgm.ht.utils.ResultModel;
import com.lgm.ht.vo.FruitVO;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@RequiredArgsConstructor
@Service
public class FruitServiceImpl implements FruitService {


    final private FruitMapper fruitMapper;

    @Override
    public ResultModel<List<FruitVO>> findByShop(Integer shopId, FruitDTO fruitDTO) {

        QueryWrapper queryWrapper = new QueryWrapper();

        queryWrapper.eq("shop_id",shopId);

        //TODO 自行处理其他过滤条件

        List<Fruit> fruitList = fruitMapper.selectList(queryWrapper);
        List<FruitVO> fruitVOList = new ArrayList<>();

        for(Fruit fruit : fruitList){
            FruitVO fruitVO = new FruitVO();

            BeanUtils.copyProperties(fruit,fruitVO);
            fruitVOList.add(fruitVO);
        }

        return ResultModel.success("查询成功",fruitVOList);
    }

    @Override
    public long findCountByType(int typeId) {

        QueryWrapper queryWrapper = new QueryWrapper();

        queryWrapper.eq("type_id",typeId);

        return fruitMapper.selectCount(queryWrapper);
    }
}
